import { Component, Injector, Input, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { ListResult, ArrayAPIBase } from 'app/modules/base/api-base';
import { DetailBase } from 'app/modules/base/detail-base';
import { Computer } from 'app/modules/computer/services/computer.service';
import { Personnel } from 'app/modules/personnel/services/personnel.service';
import { SoftwareInfo } from 'app/modules/software/services/software.service';
import { Zone } from 'app/modules/zone/services/zone.service';
import { map } from 'rxjs/operators';

import { TaskStatus, TaskTarget, TaskType, TaskService } from '../../services/task.service';
import { MatDialog } from '@angular/material';
import { from } from 'rxjs';
export class DetailsListComponent<Model> implements OnInit {
	@Input() list: Model[];
	@Input() period_type: number;
	@Input() areaCount: number;
	
	data: ArrayAPIBase<Model>;

	ngOnInit() {
		if (!this.list) {
			return;
		}
		this.data = new ArrayAPIBase(this.list);
	}
}

@Component({
	selector: 'm-task-computer-list',
	templateUrl: './details-computer-list.component.html',
})
export class DetailsComputerComponent extends DetailsListComponent<Computer> { }

@Component({
	selector: 'm-task-policy-list',
	templateUrl: './details-policy-list.component.html',
})
export class DetailsPolicyComponent extends DetailsListComponent<Computer> { }

@Component({
	selector: 'm-task-script-list',
	templateUrl: './details-script-list.component.html',
})
export class DetailsScriptComponent extends DetailsListComponent<Computer> { }



@Component({
	selector: 'm-task-personnel-list',
	templateUrl: './details-personnel-list.component.html',
})
export class DetailsPersonnelComponent extends DetailsListComponent<Personnel> { }

@Component({
	selector: 'm-task-client-list',
	templateUrl: './details-client-list.component.html',
})
export class DetailsClientComponent extends DetailsListComponent<Computer> { }

@Component({
	selector: 'm-task-software-list',
	templateUrl: './details-software-list.component.html',
})
export class DetailsSoftwareComponent extends DetailsListComponent<SoftwareInfo> { }

@Component({
	selector: 'm-task-details',
	templateUrl: './details.component.html',
	styleUrls: ['./details.component.scss'],
})
export class DetailsComponent extends DetailBase implements OnInit {
	public taskTarget = TaskTarget;
	public taskStatus = TaskStatus;
	public taskType = TaskType;

	details$ = this.route.data.pipe(map(data => data.details));
	@ViewChild('viewRef') viewRef: TemplateRef<any>;
	group;

	constructor(private injector: Injector, protected taskManager: TaskService,public dialog: MatDialog,) {
		super(injector);
	}

	ngOnInit() {
		this.details$.subscribe(details => {
		});
	}

	updateTaskStatus(ids: Number[], status: TaskStatus) {
		for (let id of ids) {
			this.taskManager.update({
				job_id: id,
				status: status,
			});
		}

		setTimeout(function (page) {
			page.router.navigate([], {
				relativeTo: page.route,
				queryParams: { time: "" },
				queryParamsHandling: 'merge',
			});
		}, 250, this);
	}

	pauseTask(ids: Number[]) {
		this.updateTaskStatus(ids, TaskStatus.Pause);
	}

	resumeTask(ids: Number[]) {
		this.updateTaskStatus(ids, TaskStatus.Running);
	}

	cancelTask(ids: Number[]) {
		this.updateTaskStatus(ids, TaskStatus.Cancel);
	}

	canCancel(job: any) {
		if (job.status != this.taskStatus.Cancel &&
			job.status != this.taskStatus.Failed &&
			job.status != this.taskStatus.Finish) {
			return true;
		}
		return false;
	}
	onView(e) {
		this.dialog.open(this.viewRef);
		this.group = e
	  }
}
